package com.example.aitoedu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aitoedu.dto.CreateSignDTO;
import com.example.aitoedu.entity.Course;
import com.example.aitoedu.entity.StuSign;
import com.example.aitoedu.entity.TeaSign;
import com.example.aitoedu.mapper.CourseMapper;
import com.example.aitoedu.mapper.ScMapper;
import com.example.aitoedu.mapper.StuSignMapper;
import com.example.aitoedu.mapper.TeaSignMapper;
import com.example.aitoedu.service.TeaSignService;
import com.example.aitoedu.vo.StuVO;
import com.example.aitoedu.vo.TeaSignVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 曹正豪
* @description 针对表【tea_sign(老师发布签到信息)】的数据库操作Service实现
* @createDate 2024-12-29 13:30:48
*/
@Service
public class TeaSignServiceImpl extends ServiceImpl<TeaSignMapper, TeaSign>
    implements TeaSignService {

    @Autowired
    private TeaSignMapper teaSignMapper;

    @Autowired
    private ScMapper scMapper;

    @Autowired
    private StuSignMapper stuSignMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void publish(CreateSignDTO createSignDTO) {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<Long> sids = scMapper.selectSidByCourseId(createSignDTO.getCid());
        LocalDateTime now = LocalDateTime.now();
        TeaSign teaSign = TeaSign.builder()
                .cid(createSignDTO.getCid())
                .endTime(createSignDTO.getEndTime())
                .type(createSignDTO.getType())
                .isEnd(0)
                .position(createSignDTO.getPosition())
                .createTime(now)
                .tid(tid)
                .build();
        teaSignMapper.insert(teaSign);
        Long maxId = teaSignMapper.maxId();
        List<StuSign> stuSignList = new ArrayList<>();
        for (Long sid: sids){
            StuSign stuSign = StuSign.builder()
                    .sid(sid)
                    .signId(maxId)
                    .isSign(0)
                    .build();
            stuSignList.add(stuSign);
        }
        stuSignMapper.insert(stuSignList);

    }

    @Override
    public List<TeaSignVO> stuSign() {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<TeaSignVO> teaSignVOList = new ArrayList<>();
        List<TeaSign> teaSignList = teaSignMapper.selectList(new QueryWrapper<TeaSign>().eq("tid", tid));
        for (TeaSign teaSign: teaSignList){
            TeaSignVO teaSignVO = new TeaSignVO();
            teaSignVO.setTeasign(teaSign);
            List<StuVO> stuVOList = teaSignMapper.stuVO(teaSign.getId());
            Course course = courseMapper.selectById(teaSign.getCid());
            teaSignVO.setCourse(course);
            teaSignVO.setStuVOList(stuVOList);
            teaSignVOList.add(teaSignVO);
        }
        return teaSignVOList;
    }
}




